import { IKey, KeyBox } from "./KeyBox";

export class KeyEditor {
    private dlg: Isc.Dialog;
    private key: IKey;
    private keyForm: Isc.DynamicForm;
    private genPassForm: Isc.DynamicForm;
    private keyBox: KeyBox;
    private newKey: boolean = false;

    constructor( keyBox: KeyBox, key?: IKey) {
        this.key = key;
        this.keyBox = keyBox;
        if ( key == null) {
            this.key = keyBox.createNewKey();
            this.newKey = true;
        }
    }

    public show() {
        this.keyForm = isc.DynamicForm.create({
            colWidths: [ "150", "300"],
            items: [
                {
                    name: "url",
                    required: true,
                    title: "地址",
                    type: "text",
                    value: this.key.url,
                    width: 300,
                },
                {
                    name: "desc",
                    title: "描述",
                    type: "text",
                    value: this.key.desc,
                    width: 300,
                },
                {
                    name: "user",
                    required: true,
                    title: "登录名",
                    type: "text",
                    value: this.key.user,
                    width: 300,
                },
                {
                    name: "password",
                    required: true,
                    title: "输入密码",
                    type: "text",
                    value: this.keyBox.decrypt( this.key.password),
                    width: 300,
                },
            ],
        });

        this.genPassForm = isc.DynamicForm.create({
            colWidths: [ 150, "*", 150, "*"],
            items: [
                {
                    click: this.genRandomPass,
                    colSpan: 4,
                    editorType: "ButtonItem",
                    left: 150,
                    name: "genPasswd",
                    title: "或随机生成密码",
                },
                {
                    colSpan: 4,
                    name: "length",
                    title: "随机密码长度",
                    type: "integer",
                    value: 16,
                    width: 300,
                },
                {
                    align: "right",
                    name: "number",
                    showTitle: false,
                    title: "数字",
                    type: "boolean",
                    value: true,
                },
                {
                    align: "right",
                    name: "special",
                    showTitle: false,
                    title: "特殊字符",
                    type: "boolean",
                    value: true,
                },
            ],
            numCols: 4,
        });

        const vlayout = isc.VLayout.create({
            border: "2px solid gray",
            members: [ this.keyForm, this.genPassForm],
        });
        this.dlg = isc.Dialog.create({
            buttons: [
                isc.Button.create( { title: "保存", click: this.save}),
                isc.Button.create( { title: "取消", click: this.cancel}),
            ],
            height: 280,
            isModal: true,
            members: [
                vlayout,
            ],
            width: 600,
        });
    }

    private save = () => {
        if ( this.keyForm.validate()) {
            this.key.desc = this.keyForm.getItem( "desc").getValue();
            this.key.url = this.keyForm.getItem( "url").getValue();
            this.key.user = this.keyForm.getItem( "user").getValue();
            this.key.password = this.keyBox.encrypt( this.keyForm.getItem( "password").getValue());
            this.key.lastUpdated = new Date().toLocaleDateString();
            this.keyBox.saveKey( this.key, this.newKey);
            this.dlg.close();
        }
        return false;
    }
    private cancel = () => {
        this.dlg.close();
        return false;
    }

    private genRandomPass = ( form: Isc.DynamicForm, item: Isc.FormItem): boolean => {
        const len = form.getItem( "length").getValue();
        const useNumber = form.getItem( "number").getValue();
        const useSpecial = form.getItem( "special").getValue();
        let possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        if ( useNumber) {
            possible += "0123456789";
        }
        if ( useSpecial) {
            possible += "`~!@#$%^&*()-+=_,.?";
        }
        let text = "";
        for (let i = 0; i < len; i++) {
            text += possible.charAt(Math.floor(Math.random() * possible.length));
        }
        this.keyForm.getItem( "password").setValue( text);
        return false;
    }
}
